package com.menu.Service.Impl;


import com.menu.Mapper.UserMapper;
import com.menu.Pojo.SecurityUser;
import com.menu.Pojo.User;
import com.menu.Utils.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


@Service
public class UserDetailsImpl implements UserDetailsService {

    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String account) throws UsernameNotFoundException {
        User user = null;
        if (account.matches(CommonUtil.email)){
            user = userMapper.selectByAccountOrEmail(null,account);
        }else{
            user = userMapper.selectByAccountOrEmail(account,null);
        }
        if (user == null) {
            throw new UsernameNotFoundException("账号不存在！");
        }
        List<String> list = new ArrayList<>();
        list.add(user.getGrade() + "");
        SecurityUser securityUser = new SecurityUser(user.getUserId(),account,user.getPassword(), list);
        return securityUser;
    }
}
